package com.carlinfo.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.carlinfo.common.dao.impl.BaseDaoImpl;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.dao.IKingDao;
import com.carlinfo.pojo.AKing;

public class KingDaoImpl extends BaseDaoImpl<AKing> implements IKingDao
{

	@Override
	public int saveOneDao(AKing king)
	{
		/* 准备一个容器 把参数放到容器中 */
		List<Object> paramsList = new ArrayList<Object>();
		/* 添加一个皇上 */
		String sql = "insert into a_king values (null,?,?,?,?,?,?,?,?,?,?)";
		paramsList.add(king.getName());
		paramsList.add(king.getAge());
		paramsList.add(king.getHeight());
		paramsList.add(king.getMiaoHao());
		paramsList.add(king.getNianHao());
		paramsList.add(king.getContent());
		paramsList.add(king.getStatus());
		paramsList.add(king.getCreateTime());
		paramsList.add(king.getUpdateTime());
		paramsList.add(king.getPubTime());

		Map<String, Object> resultMap = new HashMap<String, Object>();

		int res = super.executeJDBC(sql, paramsList, resultMap);

		king.setId(Integer.valueOf(resultMap.get("id") + ""));

		return res;
	}

	@Override
	public int updateOneDao(AKing king)
	{
		List<Object> paramsList = new ArrayList<Object>();
		String sql = " update a_king ste name = ? age = ? , height = ? , miaoHao = ? , nianHao = ? ,"
				+ " content = ? , status = ? , createTime = ? , updateTime = ? , pubTime = ? " + " where id = ?  ";
		paramsList.add(king.getName());
		paramsList.add(king.getAge());
		paramsList.add(king.getHeight());
		paramsList.add(king.getMiaoHao());
		paramsList.add(king.getNianHao());
		paramsList.add(king.getContent());
		paramsList.add(king.getStatus());
		paramsList.add(king.getCreateTime());
		paramsList.add(king.getUpdateTime());
		paramsList.add(king.getPubTime());

		paramsList.add(king.getId());
		return super.executeJDBC(sql, paramsList);
	}

	@Override
	public int deleteOneDao(Map<String, Object> condMap)
	{

		/* 准备一个容器 存放参数 */
		List<Object> paramsList = new ArrayList<Object>();

		String sql = " delete from a_king ";
		/* 按照ID查询 */
		if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
		{
			/* 判断 条件不能为空 不能为空字符串 */
			sql += " where id = ? ";
			paramsList.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
		{
			/* 填写的name值不能为空 不能为空字符串 */
			sql += " where name = ? ";
			paramsList.add(condMap.get("name"));
		}
		return super.executeJDBC(sql, paramsList);
	}

	@Override
	public AKing selectOneDao(Map<String, Object> condMap)
	{
		/* 准备一个容器 存放参数 */
		List<Object> paramsList = new ArrayList<Object>();

		String sql = " select * from a_king ";
		/* 按照ID查询 */
		if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
		{
			/* 判断 条件不能为空 不能为空字符串 */
			sql += " where id = ? ";
			paramsList.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
		{
			/* 填写的name值不能为空 不能为空字符串 */
			sql += " where name = ? ";
			paramsList.add(condMap.get("name"));
		}
		/* 调用 方法的时候先初始化 pojo */
		this.pojoObj = new AKing();

		return super.findOneJDBC(sql, paramsList);
	}

	@Override
	public List<AKing> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 准备一个方参数的容器 */
		List<Object> paramsList = new ArrayList<Object>();

		List<AKing> kingList = new ArrayList<AKing>();
		/* sql语句 */
		String sql = " select * from a_king where 1 = 1 ";
		/* 按照关键字查询 */
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			sql += " and (name like ? or miaoHao like ? or nianhao like ? ) ";
			/* 为占位符赋值 */
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
		}
		/* 按照状态 */
		if (condMap.get("status") != null && !"".equalsIgnoreCase("status"))
		{
			sql += " and status = ? ";
		}
		/* 按照时间查询 */
		if (condMap.get("stDate") instanceof Date && condMap.get("edDate") instanceof Date)
		{
			sql += " and (pubTime >= ? and pubTime < ?) ";
			paramsList.add(condMap.get("stDate"));
			paramsList.add(condMap.get("stDate"));
		}
		/* 排序 */
		if ("idAsc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:升序 */
			sql += " order by id asc";
		} else if ("idAscNameDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:升序 */
			sql += " order by id asc, name desc";
		} else
		{
			/* 默认的效果 */
			sql += " order by pubTime desc";
		}
		/* 调用 方法的时候先初始化 pojo */
		this.pojoObj = new AKing();
		/* 分页 */
		if (pageInfoUtil != null)
		{
			return super.findListByPage(pageInfoUtil, sql, paramsList);
		}
		return super.findList(sql, paramsList);
	}

}
